package mobi.springtour.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mobi.springtour.R;
import mobi.springtour.domain.Ticket;
import mobi.springtour.util.AsyncImageLoader.ImageCallback;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TicketListAdapter extends ArrayAdapter<Ticket> {

	public TicketListAdapter(Context context, List<Ticket> objects) {
		super(context, 0, objects);
	}

	private AsyncImageLoader imageLoader = new AsyncImageLoader();

	private Map<Integer, View> viewMap = new HashMap<Integer, View>();

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = this.viewMap.get(position);

		if (rowView == null) {
			LayoutInflater inflater = ((Activity) this.getContext())
					.getLayoutInflater();
			rowView = inflater.inflate(R.layout.ticket, null);
			Ticket ticket = (Ticket) this.getItem(position);

			TextView nameView = (TextView) rowView
					.findViewById(R.id.ticketName);
			nameView.setText(ticket.getTicketName());
			TextView priceView = (TextView) rowView
					.findViewById(R.id.ticketPrice);
			priceView.setText(ticket.getTicketPrice());

			final ImageView imageView = (ImageView) rowView
					.findViewById(R.id.ticketImg);
			imageLoader.loadDrawable(ticket.getTicketImg(),
					new ImageCallback() {

						public void imageLoaded(Drawable imageDrawable,
								String imageUrl) {
							imageView.setImageDrawable(imageDrawable);
						}
					});
			viewMap.put(position, rowView);
		}
		return rowView;
	}
}
